import 'package:flutter/services.dart';

class DividerInputFormatter extends TextInputFormatter {
  int first, rear; //第一个分割位数,后面分割位,,数
  String pattern; //分割符
  int countryCode; //国家码

  DividerInputFormatter({this.first = 3, this.rear = 4, this.pattern = ' ',this.countryCode = 1});



  @override
  TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {

    if(countryCode == 1){
      first = 3;
      rear = 3;
    }else if(countryCode == 86){
      first = 3;
      rear = 4;
    }else if(countryCode == 65){
      first = 4;
      rear = 4;
    }
    print('oldValue.text-----${oldValue.text}');

    print('newValue.text-----${newValue.text}');

    //不含分隔符的文本
    String allTextPure = newValue.text.replaceAll(RegExp(pattern), '');
    //处理后含分隔符的文本
    String allTextDeal = '';
    //光标位置
    int cursorPosition = newValue.selection.baseOffset;
    for (int i = 0; i < allTextPure.length; i++) {
      if ((i == first || (i - first) % rear == 0 && (i - first) / rear == 1) && allTextPure[i] != pattern) {
        allTextDeal = '$allTextDeal$pattern';
      }
      allTextDeal += allTextPure[i];
    }

    ///光标在文字最右侧(尾部)的情况,光标始终在最后
    if (cursorPosition >= newValue.text.length) {
      return TextEditingValue(
        text: allTextDeal,
        selection: TextSelection.collapsed(offset: allTextDeal.length),
      );
    }

    ///光标不在文字尾部的情况
    //如果原光标位置变为分割符,则说明左侧增加一位,光标也加一位
    if (allTextDeal.substring(cursorPosition - 1, cursorPosition) == pattern) {
      cursorPosition++;
    }
    return TextEditingValue(
      text: allTextDeal,
      selection: TextSelection.collapsed(offset: cursorPosition),
    );
  }
}
